# Copyright (C) 2002-2006, 2009, 2011 The Written Word, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# $Id: tar.py 790 2012-03-17 04:36:10Z china $

import os
import string
import sys

import misclib
import prog
import prog.archive

from prog.archive import Archive, pipe_two_commands

class Archive (Archive):
  def unpack (self, dry_run, verbose, callback, dest = None,
              filelist = None, list_files = False):
    if not dry_run:
      self.verify_md5_sig ()

    _gtar_path = misclib.find_path_prog ('gtar', self.environ)
    if not _gtar_path:
      raise prog.ProgProgramNotFound ('gtar')

    if verbose:
      _gtar_flags = 'xvf'
    else:
      _gtar_flags = 'xf'

    if verbose:
      callback ('msg', '$ ' + _gtar_path + ' ' + _gtar_flags +
                ' ' + os.path.normpath (self.path) + '\n', 2)
    if not dry_run:
      if os.spawnve (os.P_WAIT, _gtar_path, [_gtar_path, _gtar_flags,
                                             self.path], self.environ):
        raise prog.ProgArchiveError ('error gtar to unpack source')

class ArchiveBzip2 (Archive):
  def unpack (self, dry_run, verbose, callback, dest = None,
              filelist = None, list_files = False):
    if not dry_run:
      self.verify_md5_sig ()

    _bzip2_path = misclib.find_path_prog ('bzip2', self.environ)
    if not _bzip2_path:
      raise prog.ProgProgramNotFound ('bzip2')

    _gtar_path = misclib.find_path_prog ('gtar', self.environ)
    if not _gtar_path:
      raise prog.ProgProgramNotFound ('gtar')

    if verbose:
      _gtar_flags = 'jxvf'
    else:
      _gtar_flags = 'jxf'

    if verbose:
      callback ('msg', '$ ' + _gtar_path + ' ' + _gtar_flags + ' ' +
                os.path.normpath (self.path) + '\n', 2)
    if not dry_run:
      if os.spawnve (os.P_WAIT, _gtar_path, [_gtar_path, _gtar_flags,
                                             self.path], self.environ):
        raise prog.ProgArchiveError ('error using gtar to unpack source')

class ArchiveGzip (Archive):
  def unpack (self, dry_run, verbose, callback, dest = None,
              filelist = None, list_files = False):
    if not dry_run:
      self.verify_md5_sig ()

    _gzip_path = misclib.find_path_prog ('gzip', self.environ)
    if not _gzip_path:
      raise prog.ProgProgramNotFound ('gzip')

    _gtar_path = misclib.find_path_prog ('gtar', self.environ)
    if not _gtar_path:
      raise prog.ProgProgramNotFound ('gtar')

    if verbose:
      _gtar_flags = 'zxvf'
    else:
      _gtar_flags = 'zxf'

    if verbose:
      callback ('msg', '$ ' + _gtar_path + ' ' + _gtar_flags + ' ' +
                os.path.normpath (self.path) + '\n', 2)
    if not dry_run:
      if os.spawnve (os.P_WAIT, _gtar_path, [_gtar_path, _gtar_flags,
                                             self.path], self.environ):
        raise prog.ProgArchiveError ('error using gtar to unpack source')

class ArchiveLZMA (Archive):
  def unpack (self, dry_run, verbose, callback, dest = None,
              filelist = None, list_files = False):
    if not dry_run:
      self.verify_md5_sig ()

    _xz_path = misclib.find_path_prog ('xz', self.environ)
    if not _xz_path:
      raise prog.ProgProgramNotFound ('xz')

    _gtar_path = misclib.find_path_prog ('gtar', self.environ)
    if not _gtar_path:
      raise prog.ProgProgramNotFound ('gtar')

    if verbose:
      _gtar_flags = 'Jxvf'
    else:
      _gtar_flags = 'Jxf'

    if verbose:
      callback ('msg', '$ ' + _gtar_path + ' ' + _gtar_flags + ' ' +
                os.path.normpath (self.path) + '\n', 2)
    if not dry_run:
      if os.spawnve (os.P_WAIT, _gtar_path, [_gtar_path, _gtar_flags,
                                             self.path], self.environ):
        raise prog.ProgArchiveError ('error using gtar to unpack source')

